package com.wen.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author S.W.L
 * @Date 2022/8/5 15:12
 * @PackageName:com.wen.common.valid
 * @ClassName: ListValueConstraintValidator
 * @Description: TODO
 * @Version 1.0
 **/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set = new HashSet<>();

    /**
    * @Description: 初始化方法
    * @Param: [constraintAnnotation]
    * @return: void
    * @Author: S.W.L
    * @Date: 2022/8/5
    * @Time: 15:14
    */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] nums = constraintAnnotation.num();
        for (int num : nums) {
            set.add(num);
        }
    }

    /**
    * @Description: 是否校验成功
    * @Param: [value, context]
    * @return: boolean
    * @Author: S.W.L
    * @Date: 2022/8/5
    * @Time: 15:14
    */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
